/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.control;

import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.scene.Spatial;
import com.ourgame.tankgame.behavior.LifeBehavior;
import com.ourgame.tankgame.bullet.Bullet;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于决定游戏中那些东西该被销毁
 * @author L
 */
public class DeathManager {

    public List<LifeBehavior> lifeList = new ArrayList<LifeBehavior>();

    public void add(LifeBehavior lifeBehavior) {
        lifeList.add(lifeBehavior);
    }

    private static boolean processLifeObj(Spatial lifeSpatial, Spatial bulletSpatial) {
        LifeBehavior lifeObj = ((LifeBehavior) lifeSpatial);
        Bullet bullet = ((Bullet) bulletSpatial);
        int curLife = lifeObj.getCurLife();
        if (curLife > 1) {
            curLife -= bullet.getBulletAttribute().getBulletPower();
            lifeObj.setCurLife(curLife);
        } else {
            lifeObj.getBody().removeFromParent();
            return true;
        }

        if (curLife < 1) {
            lifeObj.getBody().removeFromParent();
            return true;
        }
        return false;
    }

    /**
     * 
     * @param event
     * @param pco
     * @return 
     */
    public static boolean processCollision(PhysicsCollisionEvent event) {
        PhysicsCollisionObject objectA = event.getObjectA();
        PhysicsCollisionObject objectB = event.getObjectB();

//        if (objectA == pco || objectB == pco) {
            Spatial entryA = (Spatial) objectA.getUserObject();
            Spatial entryB = (Spatial) objectB.getUserObject();

            if (entryA instanceof LifeBehavior && entryB instanceof Bullet) {
                return processLifeObj(entryA, entryB);
            } else if (entryB instanceof LifeBehavior && entryA instanceof Bullet) {
                return processLifeObj(entryB, entryA);
            }
            return false;
//        } else {
//            return false;
//        }
    }
}
